
(function($) {

$.widget("ui.datacell", {

	_init: function() {
		var self = this,
			options = this.options;
		
		this.element.addClass("ui-widget-content");
	},

	destroy: function() {
		this.element.removeClass("ui-widget-content ui-state-highlight ui-state-hover");
	},

	val: function(value) {
		if(value === undefined)
			return this.element.text();
		this.element.text(value);
	}
});

$.extend($.ui.datacell, {
	version: "@VERSION",
	defaults: {
		
	}
});

$.widget("ui.datarow", {

	_init: function() {
		var self = this,
			options = this.options;

		this.cells = Array();
		this.element.find("td").each(function() {
			$(this).datacell();
		});

		this.element.bind("mouseenter.jquicdt mouseleave.jquicdt", function() {
			$(this).find("td").toggleClass("ui-state-hover");
		});
		this.element.bind("click.jquicdt", function(event) { self._select(event) });

	},

	destroy: function() {
		this.element.unbind("mouseenter.jquicdt mouseleave.jquicdt click.jquicdt");
		this.element.find("td").each(function() {
			$(this).datacell("destroy");
		});
	},

	get: function(index) {
		return this.element.get(index);
	},

	_select: function(event) {
		if(this.options.seltype == $.ui.datatable.NONE)
			return;
		this.element.find("td").addClass("ui-state-highlight");
		if(this.options.seltype == $.ui.datatable.SINGLE ||
			this.options.seltype == $.ui.datatable.MULTI && !event.ctrlKey)
			this.element.siblings().find("td").removeClass("ui-state-highlight");
	}
});

$.extend($.ui.datacell, {
	version: "@VERSION",
	getter: "get",
	defaults: {

	}
});

$.widget("ui.datacol", {

});

$.extend($.ui.datacol, {
	version: "@VERSION",
	defaults: {
	
	}
})

$.widget("ui.datatable", {

	_init: function() {
		var self = this,
			options = this.options;
			
		this.element.addClass("ui-datatable ui-widget ui-widget-content ui-corner-all ui-helper-noselect");

		this.table = this.element.find("table");
		if(this.table.get(0))
			this._buildExisting(this.table);

		this._setTitle(options.title);
		this._updateWidth(options.width);
	},

	_setTitle: function(title) {
		if(title == "" && this.title)
			this.title.hide();
		else if(title != "" && this.title)
			this.title.text(title)
		else if(title != "")
			this.title = $("<div></div>")
				.addClass("ui-widget-header")
				.text(title)
				.prependTo(this.element);
	},

	_buildExisting: function(table) {
		var self = this,
			options = this.options;

		this.existing = true;
		table.attr("cellpadding", "0px")
			.attr("cellspacing", "0px")
			.addClass("ui-helper-reset");
		table.find("th")
			.addClass("ui-state-default");
		table.find("tbody tr").each(function() {
			$(this).datarow({
				seltype: options.selstyle
			});
		});
	},

	_updateWidth: function(value) {
		var hid = false;
		if(value === "auto") {
			if(this.element.is(":hidden")) {
				hid = true;
				this.element.show();
			}
			this.element.width("100%");
			this.element.width(this.table.width());
			if(hid)
				this.element.hide();
		}
		else {
			this.element.width(value);
			this.table.width(value);
		}
	}
});

$.extend($.ui.datatable, {
	version: "@VERSION",
	NONE: 0,
	SINGLE: 1,
	MULTI: 2,
	defaults: {
		title: "",
		width: "auto",
		selstyle: 2
	}
});

})(jQuery);
